﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.SyntaxEditor.IntelliPrompt.Implementation;
using ActiproSoftware.Text.Languages.CSharp.Implementation;
using ActiproSoftware.Text.Languages.DotNet.Reflection;
using System.ComponentModel;
using ActiproSoftware.Text;
using ActiproSoftware.Text.Lexing;
using ActiproSoftware.Text.Languages.DotNet;
using ActiproSoftware.Text.Parsing;
using ActiproSoftware.Text.Parsing.LLParser;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using ActiproSoftware.Windows.Controls.SyntaxEditor.EditActions;
using ActiproSoftware.Compatibility;
using ActiproSoftware.Windows.Controls.SyntaxEditor.IntelliPrompt;
using System.IO;
using AbstraX.CodeElements;

namespace CodeEditorTest
{
    public partial class MainPage : UserControl
    {
        private CSharpProjectAssembly projectAssembly;

        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            projectAssembly = new CSharpProjectAssembly("SampleBrowser");
            var assemblyLoader = new BackgroundWorker();
            assemblyLoader.DoWork += DotNetProjectAssemblyReferenceLoader;
            assemblyLoader.RunWorkerAsync();

            // Load the .NET Languages Add-on C# language and register the project assembly on it
            var language = new CSharpSyntaxLanguage();
            language.RegisterProjectAssembly(projectAssembly);

            CSharpGrammarOptions grammarOptions = new CSharpGrammarOptions();
            grammarOptions.RootNonTerminal = DotNetGrammarRootNonTerminal.CompilationUnit;

            CSharpGrammar grammar = new CSharpGrammar(grammarOptions);

            language.RegisterParser(new QueryParser(projectAssembly, grammar));

            codeEditor.Document.Language = language;

            var stream = this.GetType().Assembly.GetManifestResourceStream("CodeEditorTest.StaticClass.cs");
            var reader = new StreamReader(stream);
            var code = reader.ReadToEnd();

            projectAssembly.SourceFiles.QueueCode(language, "MyFile1.cs", code);
        }

        private void DotNetProjectAssemblyReferenceLoader(object sender, DoWorkEventArgs e)
        {
            // Add some common assemblies for reflection (any custom assemblies could be added using various Add overloads instead)
            projectAssembly.AssemblyReferences.AddMsCorLib();
            projectAssembly.AssemblyReferences.Add("System");
            projectAssembly.AssemblyReferences.Add("System.Core");
            projectAssembly.AssemblyReferences.Add("System.Xml");
            projectAssembly.AssemblyReferences.Add("System.Linq");
            projectAssembly.AssemblyReferences.Add(new DynamicAssembly());
        }
    }
}
